package com.steven.treatment_nurse.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.steven.treatment_nurse.R;
import com.steven.treatment_nurse.base.BaseActivity;
import com.steven.treatment_nurse.entity.ResultEntity;
import com.steven.treatment_nurse.net.MyCallback;
import com.steven.treatment_nurse.net.TaskNoHead;
import com.steven.treatment_nurse.utils.MacUtils;

import java.lang.ref.WeakReference;

import retrofit2.Response;

public class SplashActivity extends BaseActivity {
    private MyHandler myHandler;

    @Override
    public int intiLayout() {
        return R.layout.activity_splash;
    }

    @Override
    public void initView() {
        myHandler = new MyHandler(this);
        myHandler.sendEmptyMessageDelayed(0, 2000);
    }

    //注册平板信息
    private void registerSlab() {
        String mac_id = MacUtils.getMac(this).replace(":","-");//获取mac地址
        String type = "1";//默认是平板
        int depart_id = 2;//护士站对应的id
        String model = "123456";//型号
        int group_type = 2;//护士端填2
        TaskNoHead.getInstance().registerNurse(mac_id, type, depart_id, model, group_type).enqueue(new MyCallback<ResultEntity>() {
            @Override
            protected void onSuccess(Response response) {
                try {
                    JSONObject jsonObject = (JSONObject) JSONObject.toJSON(response.body());
                    assert jsonObject != null;
                    if (jsonObject.getInteger("code") == 0) {
                        myHandler.sendEmptyMessageDelayed(0, 2000);
                    }else {
                        Toast.makeText(SplashActivity.this,jsonObject.getString("msg"),Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            protected void onFailure(Throwable t) {

            }
        });
    }
    //静态内部类handler，防止内存泄漏
    private static class MyHandler extends Handler {

        private WeakReference<SplashActivity> mActivity;

        private MyHandler(SplashActivity activity) {
            mActivity = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Activity act = mActivity.get();
            if (act != null) {
                //引导页去掉
                act.startActivity(new Intent(act, LoginActivity.class));
                act.finish();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (myHandler != null) {
            myHandler = null;
        }
    }
}
